package pt.ua.helpdesk.client;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Class responsible for displaying full information on a ticket
 * 
 * @author Amigos do Noddy
 *
 */
public class TicketView extends Composite {

	/**
	 * Constructor
	 *
	 * @param tickId ID of the ticket to be displayed
	 */
	public TicketView(Integer tickId) {
		final Integer ticketId = tickId ;
		
		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		
		ArrayList labels = new ArrayList();

		final VerticalPanel vpMain = new VerticalPanel();
		horizontalPanel.add(vpMain);
		vpMain.setSpacing(10);

		final VerticalPanel verticalPanel = new VerticalPanel();
		vpMain.add(verticalPanel);
		verticalPanel.setSpacing(5);
		verticalPanel.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_1);
		horizontalPanel_1.setSpacing(2);

		final Label lblAutor = new Label("Autor:");
		horizontalPanel_1.add(lblAutor);
		lblAutor.setStyleName("labelTicket");

		final Label lblAutorText = new Label("");
		horizontalPanel_1.add(lblAutorText);
		lblAutorText.setStyleName("labelTicketText");

		final Label labelComma = new Label(",");
		labelComma.setWidth("10px");
		horizontalPanel_1.add(labelComma);
		labelComma.setStyleName("labelTicketText");
		
		final Label lblDataText = new Label("");
		horizontalPanel_1.add(lblDataText);
		lblDataText.setStyleName("labelTicketText") ;
		lblDataText.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final HorizontalPanel horizontalPanel_5 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_5);
		horizontalPanel_5.setSpacing(2);

		final Label lblStatus = new Label("Estado:");
		horizontalPanel_5.add(lblStatus);
		lblStatus.setStyleName("labelTicket");

		final Label lblStatusText = new Label("");
		horizontalPanel_5.add(lblStatusText);
		lblStatusText.setStyleName("labelTicketText");	

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_2);
		horizontalPanel_2.setSpacing(2);

		final Label lblTitle = new Label("Título:");
		horizontalPanel_2.add(lblTitle);
		lblTitle.setStyleName("labelTicket");

		final Label lblTitleText = new Label("");
		horizontalPanel_2.add(lblTitleText);
		lblTitleText.setStyleName("labelTicketText");

		final HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_3);
		horizontalPanel_3.setSpacing(2);

		final Label lblQuestion = new Label("Pergunta:");
		horizontalPanel_3.add(lblQuestion);
		lblQuestion.setStyleName("labelTicket");

		final Label lblQuestionText = new Label("");
		horizontalPanel_3.add(lblQuestionText);
		lblQuestionText.setStyleName("labelTicketText");
		
		labels.add(lblQuestionText);
		labels.add(lblTitleText);
		labels.add(lblStatusText);
		labels.add(lblAutorText);
		labels.add(lblDataText);

		ServiceTickets.Util.getInstance().loadFullTicketData(ticketId, new LoadFullTicketDataAsyncCallback(ticketId, labels, vpMain));

		final VerticalPanel verticalPanel_1 = new VerticalPanel();
		vpMain.add(verticalPanel_1);
		
		final HorizontalPanel horizontalPanel_6 = new HorizontalPanel();
		verticalPanel_1.add(horizontalPanel_6);

		final Label label_4 = new Label("Comentários »");
		horizontalPanel_6.add(label_4);
		label_4.setStyleName("section_title");
	}
	
	/**
	 * AsyncCallback responsible for receiving ticket info and ticket comments, and displaying them
	 * 
	 * @author Amigos do Noddy
	 *
	 */
	static class LoadFullTicketDataAsyncCallback implements AsyncCallback {
		  ArrayList objs;
		  VerticalPanel vp;
		  boolean may_add ;
		  Integer IDTicket ;
		  
		  /**
		   * Constructor
		   * 
		   * @param IDTicket ticket to be displayed
		   * @param objs ArrayList of labels to be filled with ticket info
		   * @param vp vertical panel where comments are to be placed
		   */
		  public LoadFullTicketDataAsyncCallback(Integer IDTicket, ArrayList objs, VerticalPanel vp) {
			  this.objs= objs;
			  this.vp= vp;
			  this.IDTicket = IDTicket ;
			  this.may_add = true ;
		  }
		  
		  public void onSuccess(Object object) {
			  ArrayList ticketdata = (ArrayList) object ;
			  
			  printTicketInfo((ArrayList)ticketdata.get(0));
			  printTicketComments((ArrayList)ticketdata.get(1));
		  }
		  public void onFailure(Throwable throwable) {
			  System.out.println("FALHOU!");
		  }
		  
		  /**
		   * Prints the ticket info on the labels included in objs field
		   * 
		   * @param ticket ArrayList with ticket info
		   */
		  private void printTicketInfo(ArrayList ticket)
		  {
	    	  if(ticket.size()>0)
	    	  {
	    		  for (int i = 0 ; i < objs.size(); i++)
	    		  {
	    			  Label lbl = (Label)objs.get(i);
	    			  lbl.setText(ticket.get(i).toString());
	    		  }
	    	  }
	    	  else
	    	  {
	    		  System.out.println("Nao tem Info sobre o Ticket");
	    	  }
		  }
		  
		  /**
		   * Prints all comments of the ticket in the vp field
		   * 
		   * @param commentsList ArrayList of comments belonging to the ticket
		   */
		  private void printTicketComments(ArrayList commentsList)
		  {
			  ArrayList names = (ArrayList)commentsList.get(0);
			  ArrayList dates = (ArrayList)commentsList.get(1);
			  ArrayList comments = (ArrayList)commentsList.get(2);
			  
			  VerticalPanel vpComment = new VerticalPanel() ;
			  vpComment.setSpacing(10) ;
			  
	    	  if(names.size()>0)
	    	  {
	    		  System.out.println("Tem "+ names.size() +" dados/nomes");
	    		  
	    		  
	    		  for (int i = 0 ; i < names.size(); i++)	
	    		  {
	    			  Label lblComma = new Label("\t,\t");
	    			  lblComma.setWidth("10px");
	    			  lblComma.setStyleName("label");
	    			  
	    			  VerticalPanel vptmp = new VerticalPanel() ;
	    			  vptmp.setStyleName("Comment");
	    			  vptmp.setSpacing(5);
	    			  
	    			  HorizontalPanel hp1 = new HorizontalPanel() ;
	    			  HorizontalPanel hp2 = new HorizontalPanel() ;
	    			  
	    			  Label lblDate = new Label();
	    			  lblDate.setText((String)dates.get(i));
	    			  lblDate.setStyleName("label");
	    			  
	    			  Label lblName = new Label() ;
	    			  lblName.setText((String)names.get(i));
	    			  lblName.setStyleName("label");
	    			  
	    			  hp1.add(lblDate);
	    			  hp1.add(lblComma);
	    			  hp1.add(lblName);
	    			  
	    			  Label lblcomm = new Label() ;
	    			  lblcomm.setText((String)comments.get(i));
	    			  lblcomm.setStyleName("label");
	    			  
	    			  hp2.add(lblcomm);
	    			  
	    			  vptmp.add(hp1);
	    			  vptmp.add(hp2);
	    			  
	    			  vpComment.add(vptmp);
	    		  }
	    	  }
	    	  else
	    	  {
	    		  System.out.println("No Ticket Comments");
	    		  Label lblvazio = new Label("Pedido não tem comentários de momento.");
	    		  lblvazio.setStyleName("erro");
	    		  vpComment.add(lblvazio);
	    	  }
	    	  
    		  vp.add(vpComment);
    		  
    		  final HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
    		  vpComment.add(horizontalPanel_4);
    		  horizontalPanel_4.setSpacing(5);
  				
    		  final Button btnVoltar = new Button();
    		  horizontalPanel_4.add(btnVoltar);
    		  btnVoltar.setStyleName("buttonVoltar");
    		  btnVoltar.setText("< Voltar");
  				
    		  btnVoltar.addClickListener(new ClickListener(){
  					public void onClick(Widget sender) {
  						Integer type = new Integer(Default.getSession("Tipo").toString()); 
  						if(type.intValue() == 1)
  						{
  							Default.loadComposite(new TicketsViewUser());
  						}
  						else if(type.intValue() == 2)
  						{
  								Default.loadComposite(new TicketsView());
  						}
  						else
  						{
  							Default.loadComposite(new TicketsViewPending());
  						}
  					}
    		  }) ;
  				
    		  Button btnAddCom = new Button();
    		  btnAddCom.setStyleName("button");
    		  btnAddCom.setText("Adicionar Comentário");
    		  if (((Label)objs.get(2)).getText().equals("Resolvido"))
    			  btnAddCom.setVisible(false) ;
  				
    		  if(new Integer(Default.getSession("Tipo").toString()).intValue() == 2) //assis
    		  {
  					Button btnSolved = new Button();
  	    			horizontalPanel_4.add(btnSolved);
  	    			btnSolved.setStyleName("button");
  	    			btnSolved.setText("Marcar como Resolvido");
  	    			
  	    			btnSolved.addClickListener(new TicketSolvedOnClick(IDTicket,btnAddCom,(Label)objs.get(2))) ;
  	    			
  	    			if (!((Label)objs.get(2)).getText().equals("A Resolver"))
  	    				btnSolved.setVisible(false);
    		  }
  			
    		  horizontalPanel_4.add(btnAddCom);
    			
    		  btnAddCom.addClickListener(new ClickListener() {
    				public void onClick(final Widget sender){
    					if (may_add)
    					{
    						VerticalPanel vp1 = new VerticalPanel() ;
    						vp1.add(new CommentAdd(new Integer((String)Default.getSession("IDPerson")),IDTicket));
    					
    						vp.add(vp1);
    						
    						may_add = false ;
    					}
    				}
    		  }) ;
		  }
		}
	
	static class LoadTicketDataAsyncCallback implements AsyncCallback {
		Button btn ;
		
		LoadTicketDataAsyncCallback(Button btn)
		{
			this.btn = btn ;
		}

		public void onFailure(Throwable caught) {
			System.out.println("SetTicketAsSolved :: FALHOU a alterar o estado do ticket!");
			
		}

		public void onSuccess(Object result) {
			
		}
	
	}
}